package com.geek.log4jcontrol.core;

import java.lang.reflect.Method;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.rmi.server.RemoteServer;
import java.rmi.server.ServerNotActiveException;

import com.geek.log4jcontrol.commons.ConfigConstants;
import com.geek.log4jcontrol.commons.FinalConstants;
import com.geek.log4jcontrol.core.annotation.OperatePwd;
import com.geek.log4jcontrol.core.annotation.PwdType;
import com.geek.log4jcontrol.remote.Log4jControlRemote;

/**
 * @package com.geek.log4jcontrol.core
 * @description 操作权限的校验
 * @auther Goliath
 * @createTime 2012-8-2 下午02:16:00
 * @modifyTime
 */
public class PermissionChecker {
	
	private PermissionChecker(){
	}
	
	private static PermissionChecker permissionChecker = new PermissionChecker();

	public static PermissionChecker getInstance() {
		return permissionChecker;
	}
	
	/**
	 * @package com.geek.log4jcontrol.core
	 * @description 进行RMI接口调用的权限校验动作
	 * @param remoteServer RMI服务对象
	 * @param params 接口方法的请求参数
	 * @param method 接口方法对象
	 * @return 校验是否成功 0 成功 1 失败
	 * @returnType int
	 * @author Goliath
	 * @createTime 2012-8-3 上午10:33:10
	 * @modifyTime
	 */
	@SuppressWarnings("static-access")
	public int checkPermissions(Object remoteServer, Object[] params, Method method) {
		int ipCheckFlag = FinalConstants.SUCCESS;
		if (ConfigConstants.IP_CHECK == FinalConstants.YES) {
			try {
				String clienthost = ((RemoteServer) remoteServer).getClientHost();
				InetAddress ia = InetAddress.getByName(clienthost);
				String clientIp = ia.getHostAddress();
				ipCheckFlag = checkIP(clientIp);
			} catch (ServerNotActiveException e) {
				e.printStackTrace();
			} catch (UnknownHostException e) {
				e.printStackTrace();
			}
		}
		int operatePwdCheckFlag = FinalConstants.SUCCESS;
		if (ConfigConstants.IP_CHECK == FinalConstants.YES) {
			String operatePwd = (String) params[0];
			operatePwdCheckFlag = checkoperatePwd(operatePwd, method);
		}
		return ipCheckFlag == FinalConstants.SUCCESS && operatePwdCheckFlag == FinalConstants.SUCCESS ? FinalConstants.SUCCESS
				: FinalConstants.FAIL;
	}

	/**
	 * @package com.geek.log4jcontrol.core
	 * @description 校验客户端IP是否处于白名单中
	 * @param clientIp 客户端IP
	 * @return IP是否处在白名单中
	 * @returnType int
	 * @author Goliath
	 * @createTime 2012-8-3 上午10:34:39
	 * @modifyTime
	 */
	private int checkIP(String clientIp) {
		if (!ConfigConstants.IP_WHITELIST.containsKey(clientIp)) {
			return FinalConstants.FAIL;
		}
		return FinalConstants.SUCCESS;
	}

	/**
	 * @package com.geek.log4jcontrol.core
	 * @description 校验操作码
	 * @param operatePwd 操作码
	 * @param method 接口方法对象
	 * @return 操作码校验是否通过
	 * @returnType int
	 * @author Goliath
	 * @createTime 2012-8-3 上午10:35:28
	 * @modifyTime
	 */
	private int checkoperatePwd(String operatePwd, Method method) {
		try {
			Method methodIface = Log4jControlRemote.class.getMethod(method.getName(), method.getParameterTypes());
			OperatePwd oOperatePwd = methodIface.getAnnotation(OperatePwd.class);
			PwdType oPwdType = oOperatePwd.pwdtype();
			if (oPwdType == PwdType.READ) {
				if (!operatePwd.equals(ConfigConstants.PWD_READ)) {
					return FinalConstants.FAIL;
				}
			} else if (oPwdType == PwdType.OPERATE) {
				if (!operatePwd.equals(ConfigConstants.PWD_OPERATE)) {
					return FinalConstants.FAIL;
				}
			} else if (oPwdType == PwdType.LOG) {
				if (!operatePwd.equals(ConfigConstants.PWD_LOG)) {
					return FinalConstants.FAIL;
				}
			}
		} catch (SecurityException e) {
			e.printStackTrace();
		} catch (NoSuchMethodException e) {
			e.printStackTrace();
		}
		return FinalConstants.SUCCESS;
	}
}
